package com.college.collegeinfomanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.collegeinfomanage.dto.ResearchStrengthDTO;
import com.college.collegeinfomanage.entity.ResearchStrength;
import com.college.collegeinfomanage.mapper.ResearchStrengthMapper;
import com.college.collegeinfomanage.mapper.UniversityMapper;
import com.college.collegeinfomanage.service.ResearchStrengthService;
import com.college.collegeinfomanage.vo.ResearchStrengthListVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ResearchStrengthServiceImpl extends ServiceImpl<ResearchStrengthMapper, ResearchStrength> implements ResearchStrengthService {

    @Resource
    private ResearchStrengthMapper researchStrengthMapper;

    @Resource
    private UniversityMapper universityMapper;


    @Override
    public IPage<ResearchStrengthListVO> getPage(ResearchStrengthDTO researchStrengthDTO) {
        LambdaQueryWrapper<ResearchStrength> wrapper = new LambdaQueryWrapper<>();
        if (researchStrengthDTO.getUniversityId() != null){
            wrapper.eq(ResearchStrength::getUniversityId,researchStrengthDTO.getUniversityId());
        }
        if (researchStrengthDTO.getResearchArea() != null && !"".equals(researchStrengthDTO.getResearchArea())){
            wrapper.like(ResearchStrength::getResearchArea,researchStrengthDTO.getResearchArea());
        }
        IPage<ResearchStrength> page = new Page<>(researchStrengthDTO.getPage(),researchStrengthDTO.getSize());
        page = researchStrengthMapper.selectPage(page,wrapper);
        List<ResearchStrength> researchStrengthList = page.getRecords();
        List<ResearchStrengthListVO> collect = researchStrengthList.stream()
                .map(researchStrength -> {
                    ResearchStrengthListVO researchStrengthListVO = new ResearchStrengthListVO();
                    researchStrengthListVO.setId(researchStrength.getId());
                    researchStrengthListVO.setUniversityId(researchStrength.getUniversityId());
                    researchStrengthListVO.setUniversityName(universityMapper.selectById(researchStrength.getUniversityId()).getName());
                    researchStrengthListVO.setResearchArea(researchStrength.getResearchArea());
                    researchStrengthListVO.setResearchProjects(researchStrength.getResearchProjects());
                    researchStrengthListVO.setQualityAssessment(researchStrength.getQualityAssessment());
                    return researchStrengthListVO;
                }).collect(Collectors.toList());
        IPage<ResearchStrengthListVO> page1 = new Page<>(researchStrengthDTO.getPage(), researchStrengthDTO.getSize());
        page1.setTotal(page.getTotal());
        page1.setPages(page.getPages());
        page1.setCurrent(page.getCurrent());
        page1.setSize(page.getSize());
        page1.setRecords(collect);
        return page1;
            }
}
